package com.cqc.jdk8.Jio.writer;

import org.junit.Test;

import java.io.FileWriter;

public class FileWriterTest {

    /**
     * write() append()的区别？
     * append()可以链式，write()不行，因为append()会返回当前的writer
     *
     * @throws Exception
     */
    @Test
    public void f1() throws Exception {
        FileWriter writer = new FileWriter("C:\\Users\\cui\\Desktop\\a.txt");
        writer.write("黄飞鸿");
        writer.write(System.lineSeparator());
        writer.write("男儿当自强");
        writer.append("李连杰");
        writer.close();
    }

    @Test
    public void f2() throws Exception {
        FileWriter writer = new FileWriter("C:\\Users\\cui\\Desktop\\aaaa.csv");
        System.out.println(writer.getEncoding());//u8
        //excel打开CSV中文乱码，其中一个字段的编码设为U8就解决了 new String("姓名,年龄,班级".getBytes(StandardCharsets.UTF_8))
        writer.append("姓名,年龄,班级")
                .append(System.lineSeparator())
                .append("张三,15,一班")
                .append(System.lineSeparator())
                .append("李四,16,二班")
                .flush();
        writer.close();
    }

    @Test
    public void f3() throws Exception {
        FileWriter writer = new FileWriter("C:\\Users\\cui\\Desktop\\a.csv",true);
        writer.write(System.lineSeparator());
        writer.write("王五,17,三班");
        writer.flush();
        writer.close();
    }
}
